package com.ruoyi.web.controller.training;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.pdf.PdfExporter;
import com.ruoyi.training.domain.TrainPlan;
import com.ruoyi.training.service.ITrainPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训计划管理Controller
 *
 * @author wng
 * @date 2024-08-04
 */
@RestController
@RequestMapping("/training/plan")
public class TrainPlanController extends BaseController {
    @Resource
    private ITrainPlanService trainPlanService;

    /**
     * 查询培训计划管理列表
     */
    @PreAuthorize("@ss.hasPermi('training:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainPlan trainPlan) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUserId() != 1 && trainPlan.getDeptId() == null) {
            trainPlan.setDeptId(loginUser.getDeptId());
        }
        trainPlan.setSelectByWho(loginUser.getUserId());
        List<TrainPlan> list = trainPlanService.selectTrainPlanList(trainPlan);
        return getDataTable(list);
    }

    /**
     * 导出培训计划管理列表
     */
    @PreAuthorize("@ss.hasPermi('training:plan:export')")
    @Log(title = "培训计划管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainPlan trainPlan) throws Exception{
        List<TrainPlan> list = trainPlanService.selectTrainPlanList(trainPlan);
//        ExcelUtil<TrainPlan> util = new ExcelUtil<TrainPlan>(TrainPlan.class);
//        util.exportExcel(response, list, "培训计划管理数据");
        PdfExporter<TrainPlan> util = new PdfExporter<>();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=export.pdf");
        util.exportListToPdf(list,response.getOutputStream());
    }

    /**
     * 获取培训计划管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(trainPlanService.selectTrainPlanById(id));
    }

    /**
     * 新增培训计划管理
     */
    @PreAuthorize("@ss.hasPermi('training:plan:add')")
    @Log(title = "培训计划管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainPlan trainPlan) {
        LoginUser loginUser = getLoginUser();
        trainPlan.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        trainPlan.setDeptId(loginUser.getDeptId());
        return toAjax(trainPlanService.insertTrainPlan(trainPlan));
    }

    /**
     * 修改培训计划管理
     */
    @PreAuthorize("@ss.hasPermi('training:plan:edit')")
    @Log(title = "培训计划管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainPlan trainPlan) {
        LoginUser loginUser = getLoginUser();
        trainPlan.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
        return toAjax(trainPlanService.updateTrainPlan(trainPlan));
    }

    /**
     * 删除培训计划管理
     */
    @PreAuthorize("@ss.hasPermi('training:plan:remove')")
    @Log(title = "培训计划管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(trainPlanService.deleteTrainPlanByIds(ids));
    }
}
